package com.itqhy.controller;


import com.itqhy.exception.UserException;
import com.itqhy.pojo.User;
import com.itqhy.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;

@Controller
    public class UserController {

        @Resource(name = "userService")
        private UserService userService;
    /**
     * 注册
     */
    @RequestMapping("/register")
    public String register(User user) throws UserException, IOException {
        try{
            user.setRegistTime(new Date());
            userService.register(user);

        }catch (UserException e){
           e.printStackTrace();
        }
        return "/registersuccess.jsp";
    }
    /**
     * 登录
     */
    @RequestMapping("/loginController")
    public String loginUser(HttpServletRequest request, HttpSession session,String username, String password,String remember,HttpServletResponse response) throws IOException, ServletException {
        String ck= request.getParameter("ckcode");
        String session8= (String) session.getAttribute("code");
        if(!ck.equals(session8)){
            request.setAttribute("ckcode_msg","验证码错误！");
            return  "/login.jsp";
        }
        //根据用户名和密码查询
        try {
            //记住用户名和密码
            //创建两个cookie对象
            Cookie ckname=new Cookie("username",username);
            Cookie ckpassword=new Cookie("password",password);
            if (remember!=null){
                ckname.setMaxAge(Integer.MAX_VALUE);//设置cookie的存活时间为最长
                ckpassword.setMaxAge(Integer.MAX_VALUE);
                response.addCookie(ckname);
                response.addCookie(ckpassword);
            }else {
                ckname.setMaxAge(0);
                ckpassword.setMaxAge(0);
            }


            User user=userService.loginByUsernameAndPasswoerd(username,password);
            session.setAttribute("user",user);
        } catch (UserException e) {
            session.setAttribute("user_msg",e.getMessage());
            return "/login.jsp";
        }
        return "/index.jsp";
    }
    /**
     * 查看我的账户
     */
    @RequestMapping(value = "/myAccout",produces = "text/html;charset=UTF-8")
    public String myAccout() throws IOException {

        return "/myAccount.jsp";

    }

    /**
     * 修改用户信息
     */
    @RequestMapping("updateUser")
    public String updateUserById(User user,HttpServletRequest request){

        try {

            userService.updateUserById(user);
            request.getSession().invalidate();

        } catch (UserException e) {

            e.printStackTrace();

        }
        return "/modifyUserInfoSuccess.jsp";
    }
    /**
     * 用户退出
     */
    @RequestMapping("/exitUser")
    public String exitUser(HttpServletRequest request) throws IOException {

        request.getSession().invalidate();
        return "/userexitsuccess.jsp";


    }



}
